package com.sxfq.loansupermarket.backsystem.modular.guestDimension.controller;

import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.IntegralMall.entity.MembersUser;
import com.sxfq.loansupermarket.backsystem.modular.IntegralMall.service.ISendMessageService;
import com.sxfq.loansupermarket.backsystem.modular.guestDimension.entity.GuestDimensionInfo;
import com.sxfq.loansupermarket.backsystem.modular.guestDimension.service.IGuestDimensionService;
import com.sxfq.loansupermarket.backsystem.modular.websocket.WebSocketWxChat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客维
 *
 * @author py
 * @Date 2020年1月9日21:15:54
 */
@Controller
@RequestMapping("/guestDimension")
public class GuestDimensionController extends BaseController {
    @Autowired
    IGuestDimensionService service;

    @Autowired
    WebSocketWxChat webSocketWxChat;

    @Autowired
    private ISendMessageService sendMessageService;

    @RequestMapping(value = "/querySenderList", method = RequestMethod.GET)
    @ResponseBody
    public List<GuestDimensionInfo> querySenderList() {
        return service.querySenderList();
    }

    // 查询聊天记录by发送人
    @RequestMapping(value = "/queryChatRecordBySender", method = RequestMethod.POST)
    @ResponseBody
    public List<GuestDimensionInfo> queryChatRecordBySender(String sender) {
        return service.queryChatRecordBySender(sender);
    }

    // 查询聊天记录by发送人
    @RequestMapping(value = "/fuzzyQueryBySender", method = RequestMethod.POST)
    @ResponseBody
    public List<GuestDimensionInfo> fuzzyQuery(String sender) {
        return service.fuzzyQuery(sender);
    }

    // 查询聊天记录by接收人
    @RequestMapping(value = "/queryChatRecordByReceiver", method = RequestMethod.POST)
    @ResponseBody
    public List<GuestDimensionInfo> queryChatRecordByReceiver(String receiver) {
        return service.queryChatRecordByReceiver(receiver);
    }

    // 查询聊天记录by发送时间
    @RequestMapping(value = "/queryChatRecordBySendTime", method = RequestMethod.POST)
    @ResponseBody
    public List<GuestDimensionInfo> queryChatRecordBySendTime(String startSenderTime, String endSenderTime) {
        return service.queryChatRecordBySendTime(startSenderTime, endSenderTime);
    }

    // 客服发送聊天记录
    @RequestMapping(value = "/guestSenderChatRecord", method = RequestMethod.POST)
    @ResponseBody
    public int guestSenderChatRecord(String openId, String weChatNews) throws Exception {
        GuestDimensionInfo guestDimensionInfo = new GuestDimensionInfo();
        guestDimensionInfo.setSender(ShiroKit.getUser().getAccount());
        guestDimensionInfo.setOpenId(openId);
        guestDimensionInfo.setReceiver(openId);
        guestDimensionInfo.setChatRecord(weChatNews);
        guestDimensionInfo.setSendStatus(2);
        guestDimensionInfo.setIsNew(0);
        service.insertWxChatRecord(guestDimensionInfo);
        sendMessageService.sendTextMessage(guestDimensionInfo.getChatRecord(), guestDimensionInfo.getOpenId());
        webSocketWxChat.sendMessageWxChat(openId + "发送了消息！");
        return 1;
    }

    // get nickname和headimgurl
    @RequestMapping(value = "/getWxUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public MembersUser getWxUserInfo(String openId) {
        return service.getWxUserInfo(openId);
    }
}